import { Injectable } from '@angular/core';
import { HttpClient, HttpParams} from '@angular/common/http';
import { Base, Hero, HeroArg, UpdateHeroArg } from '@configs/types';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { stringify } from 'qs';
import { environment } from '../../environments/environment';

@Injectable({
  providedIn: 'root'
})

export default class HeroService {
  private prefix = environment.baseUrl + '/hero/';
  constructor(private http: HttpClient) {}
  getHeroes(args: HeroArg): Observable<Hero[]> {
    const params = new HttpParams({ fromString: stringify(args) });
    return this.http.get(this.prefix + 'list', { params })
      .pipe(map((res: Base<Hero[]>) => res.data));
  }
  addHero(args: UpdateHeroArg): Observable<Base<void>> {
    return this.http.post<Base<void>>(this.prefix + 'add', args);
  }
}
